use "$temp/acs_longform_merged_clean", clear
keep if dby>=1990 //restrict to at-risk sample

tab age_group parent_inc_quart

//load in graphing programs
do "$prog/02_results_20241209_programs"

//get division, region in 2000
//use "$data/Other/state_region_xwalk", clear

ren state_2000 stfip
merge m:1 stfip using "$data/Other/state_region_xwalk", keep(match) nogen
ren stfip state_2000

keep if dby>=1990 //restrict to at-risk sample
tab age_group parent_inc_quart


//final cleaning
replace coll = 0 if age_group == 1
replace assoc = 0 if age_group == 0
drop if parent_inc_quart == .  //nix matched kids with no parent income
replace ti = . if wag == . 
replace wag = . if ti == . 


*********plotting******

//regression of CZ shocks on covariates
//local controls `"sex asian black hispan i.year i.head_coll i.age_group#i.div head_mar head_constr_manu house_value parent_inc own_house"'
replace house_value = house_value/10000
replace parent_inc = parent_inc/10000

reg cz_shock i.div i.year i.age_group [aw=weight], robust
outreg2 using "$temp/1_balance_reg.dta",  dta replace keep() noast dec(3)

reg cz_shock i.div i.year i.age_group sex black hispan [aw=weight], robust 
outreg2 using "$temp/1_balance_reg.dta", dta  keep(sex black hispan) noast dec(3)

reg cz_shock i.div i.year i.age_group sex black hispan head_coll head_mar head_constr_manu house_value parent_inc own_house [aw=weight], robust
outreg2 using "$temp/1_balance_reg.dta", dta  keep(sex black hispan head_coll head_mar head_constr_manu house_value parent_inc own_house) noast dec(3)

reg cz_shock sex black hispan head_coll head_mar head_constr_manu house_value parent_inc own_house [aw=weight], robust
outreg2 using "$temp/1_balance_reg.dta", dta  keep(sex black hispan head_coll head_mar head_constr_manu house_value parent_inc own_house) noast dec(3)

//output
preserve
use "$temp/1_balance_reg_dta", clear
drop if _n>=4 & _n<=53
export excel using "$disc/1_balance_reg_T13.xlsx", replace
restore 

//important graphs
plotreg coll 4 60 0 1 "2" 0 //college attainment, WITH CONTROLS

//DECILE RESULTS
use "$temp/acs_longform_merged_clean", clear
keep if dby>=1990 //restrict to at-risk sample

tab age_group parent_inc_quart
ren state_2000 stfip
merge m:1 stfip using "$data/Other/state_region_xwalk", keep(match) nogen
ren stfip state_2000

qui{
	forval y = 1990/1994{
		noi di "Working on year `y'"
		xtile parent_inc_dec_`y' = parent_inc [w = weight] if dby == `y', nq(10)
	}
}


//final cleaning
replace coll = 0 if age_group == 1
replace assoc = 0 if age_group == 0
drop if parent_inc_quart == .  //nix matched kids with no parent income
replace ti = . if wag == . 
replace wag = . if ti == . 

egen parent_inc_dec = rowmax(parent_inc_dec_*)
replace parent_inc_dec = 5 if parent_inc_dec == .

//running regressions over deciles
qui{
local dv "coll"
forval q = 1/10{
	noi di as result "`q'"
	local controls `"i.age_group i.year"'
	areg coll age_dum* `controls' [aw = weight] if parent_inc_dec == `q', absorb(cz_2000) vce(cluster cz_2000)

	//graphing
	forval i = 1/4{
		local effect_`i'_`q' = round(_b[age_dum_`i'], 0.001)
		local se_`i'_`q' = round(_se[age_dum_`i'], 0.001)
		su `dv' [aw = weight] if age_group == `i' & parent_inc_dec == `q'
		local mean_`i'_`q' = round(`r(mean)', 0.001)
	
	}
}
}

//create data to make graph out of
clear
set obs 40
gen group = .
gen effect = .
gen se = .
gen mean = .
gen quart = .
forval q = 1/10{
	forval i = 1/4{
		local line = (`q'-1)*4 + `i'
		replace quart = `q' in `line'
		replace group = `i' in `line'
		replace effect = `effect_`i'_`q'' in `line'
		replace se = `se_`i'_`q'' in `line'
		replace mean = `mean_`i'_`q'' in `line'
	}
}

//upper, lower bounds
gen ub = effect + 1.96*se
gen lb = effect - 1.96*se
keep if group == 4	
gen effect_pct = (effect/mean)*100
gen se_pct = (se/mean)*100
serrbar effect se quart, scale(1.96) bgcolor(white) graphregion(color(white)) ///
ytitle("Percentage Effect, Age 26-28") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10") xtitle("Parent Income Decile") yline(0)
graph export "$disc/3a_coll_effects_dec_lvl_T13.png", replace

serrbar effect_pct se_pct quart, scale(1.96) bgcolor(white) graphregion(color(white)) ///
ytitle("Percentage Effect, Age 26-28") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10") xtitle("Parent Income Decile") yline(0)
graph export "$disc/3b_coll_effects_dec_pct_T13.png", replace



//middle quartile: results by house median value
use "$temp/acs_longform_merged_clean", clear
keep if dby>=1990 //restrict to at-risk sample
tab age_group parent_inc_quart
ren state_2000 stfip
merge m:1 stfip using "$data/Other/state_region_xwalk", keep(match) nogen
ren stfip state_2000
keep if parent_inc_quart == 2 | parent_inc_quart == 3

su house_value if own_house [aw=weight], d
gen hmed = 0
replace hmed = 1 if house_value>`r(p50)' & own_house

*****interaction with housing dummy
local controls `"cz_shock sex asian black hispan i.year i.state_2000#i.head_coll i.age_group head_mar head_constr_manu house_value parent_inc own_house"'

//with housing
reg coll c.cz_shock#i.age_group `controls' [aw = weight] if own_house, vce(cluster cz_2000)
outreg2 using "$temp/07_middlequart_het.dta", dta keep(c.cz_shock#i.age_group) noast dec(3) replace

reg coll c.cz_shock#i.age_group `controls' [aw = weight] if own_house & !hmed, vce(cluster cz_2000)
outreg2 using "$temp/07_middlequart_het.dta", dta keep(c.cz_shock#i.age_group) noast dec(3) 

reg coll c.cz_shock#i.age_group `controls' [aw = weight] if own_house & hmed, vce(cluster cz_2000)
outreg2 using "$temp/07_middlequart_het.dta", dta keep(c.cz_shock#i.age_group) noast dec(3) 

//output regressions
use "$temp/07_middlequart_het_dta", clear
export excel using "$disc/4_effects_hmed.xlsx", replace


